package com.airpi.activities;

import java.util.concurrent.ExecutionException;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.airpi.R;
import com.airpi.heavy_tasks.AsyncTaskActivity;

/**
 * Main activity containing the 3 instances of the Main Fragment
 * The first Fragment is reachable by sliding to the left from the Main UI, and is used to display the current MediaList 
 * The Second Fragment is displaying the Main UI with all the control buttons, and the streaming functions
 * The third Fragment is reachable by sliding to the right from the Main UI, and is used to display the current UserList
 */
public class MainActivity extends FragmentActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		MainFragmentPagerAdapter adapter = new MainFragmentPagerAdapter(getSupportFragmentManager());
		final ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setOffscreenPageLimit(2);
		mViewPager.setAdapter(adapter);

		//Actionbar			
		final ActionBar actionBar = getActionBar();
		final String[] titles = {getString(R.string.main_user_list_title), getString(R.string.main_upload_title), getString(R.string.main_media_list_title)};

		mViewPager.setOnPageChangeListener(
				new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						// When swiping between pages, select the corresponding tab.
						actionBar.setSelectedNavigationItem(position);
					}
				});	

		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			@Override
			public void onTabReselected(Tab tab, FragmentTransaction ft) {

			}

			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {	
				mViewPager.setCurrentItem(tab.getPosition());
			}

			@Override
			public void onTabUnselected(Tab tab, FragmentTransaction ft) {

			}
		};

		// Add 3 tabs, specifying the tab's text and TabListener
		for (int i = 0; i < 3; i++) {
			actionBar.addTab(
					actionBar.newTab()
					.setText(titles[i])
					.setTabListener(tabListener));
		}
		mViewPager.setCurrentItem(1); // default on main_ui

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		switch(item.getItemId()) {
		case R.id.menu_application_settings:
			startActivity(new Intent(this, ApplicationSettings.class));
			break;
		case R.id.menu_main_logout:
			AsyncTaskActivity atcLogout = new AsyncTaskActivity(this, "Logout;"+sharedPrefs.getString(LoginActivity.LOGIN_USER, null));
			atcLogout.execute();
			try {
				System.out.println(atcLogout.get());
				if(atcLogout.get()){
					startActivity(new Intent(this, LoginActivity.class));
					finish();
					break;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public class MainFragmentPagerAdapter extends FragmentStatePagerAdapter {
		public MainFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment f = null;
			switch(i) {
			case 0:
				System.out.println("case 0 : " + i);
				f = new MainLeftFragment();
				break;
			case 1:
				System.out.println("case 1 : " + i);
				f = new MainMiddleFragment();
				break;
			case 2:
				System.out.println("case 2 : " + i);
				f = new MainRightFragment();
				break;
			}
			return (Fragment)f;
		}

		@Override
		public int getCount() {
			return 3; // number of fragments that will be displayed
		}
	}
}